/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package managers;

import conexionBD.ConexionServidor;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

/**
 *
 * @author Juan Jose
 */
@ManagedBean
@SessionScoped
public class managerModificarPerfil implements Serializable {
    private TreeNode root;

    private Integer perfilSeleccionado;
    private TreeNode[] selectedNodes;
    List<SelectItem> perfiles = new ArrayList<SelectItem>();

        WebApplicationContext permisos = FacesContextUtils.getRequiredWebApplicationContext(FacesContext.getCurrentInstance());
        JdbcTemplate jdbcTemplate = (JdbcTemplate)permisos.getBean("jdbcTemplate");

        public managerModificarPerfil() {
                //this.perfilSeleccionado = "";

        root = new DefaultTreeNode("Root", null);
        TreeNode node0 = new DefaultTreeNode("SOLICITUD", root);
        TreeNode node1 = new DefaultTreeNode("EXPEDIENTE", root);
        TreeNode node2 = new DefaultTreeNode("ESTADISTICAS", root);
        TreeNode node3 = new DefaultTreeNode("PERFIL", root);
        TreeNode node4 = new DefaultTreeNode("USUARIO", root);
        TreeNode node5 = new DefaultTreeNode("MATRIZ DE RECORRIDOS", root);
        //TreeNode node6 = new DefaultTreeNode("REPORTE", root);
        TreeNode node7 = new DefaultTreeNode("MANTENIMIENTO", root);
        TreeNode node8 = new DefaultTreeNode("AUDITORIA", root);

        TreeNode node00 = new DefaultTreeNode("CREAR SOLICITUD", node0);
        TreeNode node01 = new DefaultTreeNode("MODIFICAR SOLICITUD", node0);
        TreeNode node02 = new DefaultTreeNode("CONSULTAR SOLICITUD", node0);

        TreeNode node10 = new DefaultTreeNode("CREAR EXPEDIENTE", node1);
        TreeNode node11 = new DefaultTreeNode("MODIFICAR EXPEDIENTE", node1);
        TreeNode node12 = new DefaultTreeNode("CONSULTAR EXPEDIENTE", node1);
        TreeNode node13 = new DefaultTreeNode("CONTROL DE FECHAS", node1);
        TreeNode node14 = new DefaultTreeNode("ASIGNAR ESTADO EXPEDIENTE", node1);
        TreeNode node15 = new DefaultTreeNode("CREAR REPORTE", node1);
        TreeNode node16 = new DefaultTreeNode("ASIGNAR VISTO BUENO", node1);

        TreeNode node20 = new DefaultTreeNode("CONSULTAR ESTADISTICA", node2);

        TreeNode node30 = new DefaultTreeNode("CREAR PERFIL", node3);
        TreeNode node31 = new DefaultTreeNode("MODIFICAR PERFIL", node3);
        TreeNode node32 = new DefaultTreeNode("CONSULTAR PERFIL", node3);

        TreeNode node40 = new DefaultTreeNode("CREAR USUARIO", node4);
        TreeNode node41 = new DefaultTreeNode("MODIFICAR DATOS PERSONALES", node4);
        TreeNode node42 = new DefaultTreeNode("MODIFICAR PERFIL USUARIO", node4);
        TreeNode node43 = new DefaultTreeNode("ESTADO DE USUARIO", node4);

        TreeNode node50 = new DefaultTreeNode("CREAR MATRIZ DE RECORRIDOS", node5);

        TreeNode node70 = new DefaultTreeNode("MODIFICAR MANTENIMIENTO", node7);
        TreeNode node80 = new DefaultTreeNode("CONSULTAR AUDITORIA", node8);
	}

	public TreeNode getRoot() {
		return root;
	}

	public TreeNode[] getSelectedNodes() {
		return selectedNodes;
	}

	public void setSelectedNodes(TreeNode[] selectedNodes) {
		this.selectedNodes = selectedNodes;
	}

	public void displaySelectedMultiple(ActionEvent event) {
                borrar();
                entrar_sql("ROLE_PASO");
                StringBuilder builder = new StringBuilder();

		for(TreeNode node : selectedNodes) {
			insertar_roles(node.getData().toString());
                        builder.append(node.getData().toString());
			builder.append("<br />");
		}

		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Seleccionados", builder.toString());

		FacesContext.getCurrentInstance().addMessage(null, message);
	}


    public List<SelectItem> getPerfiles() {
        obtenerNombrePerfil();
        ConexionServidor myConexion = new ConexionServidor();
        List<SelectItem> perfiles = new ArrayList<SelectItem>();
        perfiles.add(new SelectItem("0", "-seleccione uno-"));
        try {
            Connection con = myConexion.getConnection();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT IDPERFIL, NOMBREPERFIL FROM PERFIL WHERE NOMBREPERFIL != 'ADMINISTRADOR'");
            while (rs.next()) {
                perfiles.add(new SelectItem(rs.getInt("idperfil"), rs.getString("nombreperfil")));
            }
            rs.close();
            st.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
       
        return perfiles;
     }


    public Integer getPerfilSeleccionado() {
        return perfilSeleccionado;
    }

    public void setPerfilSeleccionado(Integer perfilSeleccionado) {
         this.perfilSeleccionado = perfilSeleccionado;
    }
    public void setPerfiles(List<SelectItem> perfiles) {
        this.perfiles = perfiles;
    }


public void insertar_roles(String x) {
        String a = "CREAR EXPEDIENTE";
        String b = " ";
        if (a.equals(x)) {
            a = "ROLE_CREAREXPE";
            b = "CREAR EXPEDIENTE";
            insert_sql(a, b);
        }
        a = "MODIFICAR EXPEDIENTE";
        if (a.equals(x)) {
            a = "ROLE_MODEXPE";
            b = "MODIFICAR EXPEDIENTE";
            insert_sql(a, b);
        }
        a = "CONSULTAR EXPEDIENTE";
        if (a.equals(x)) {
            a = "ROLE_CONSUEXPE";
            b = "CONSULTAR EXPEDIENTE";
            insert_sql(a, b);
        }
        a = "CREAR SOLICITUD";
        if (a.equals(x)) {
            a = "ROLE_CREASOLI";
            b = "CREAR SOLICITUD";
            insert_sql(a, b);
        }
        a = "MODIFICAR SOLICITUD";
        if (a.equals(x)) {
            a = "ROLE_MODSOLI";
            b = "MODIFICAR SOLICITUD";
            insert_sql(a, b);
        }
        a = "CONSULTAR SOLICITUD";
        if (a.equals(x)) {
            a = "ROLE_CONSUSOLI";
            b = "CONSULTAR SOLICITUD";
            insert_sql(a, b);
        }
        a = "CONTROL DE FECHAS";
        if (a.equals(x)) {
            a = "ROLE_CONTROLFECHA";
            b = "CONTROL DE FECHAS";
            insert_sql(a, b);
        }
        a = "CONSULTAR ESTADISTICA";
        if (a.equals(x)) {
            a = "ROLE_CONSUESTA";
            b = "CONSULTAR ESTADISTICA";
            insert_sql(a, b);
        }
        a = "CREAR PERFIL";
        if (a.equals(x)) {
            a = "ROLE_CREARPERF";
            b = "CREAR PERFIL";
            insert_sql(a, b);
        }
        a = "MODIFICAR PERFIL";
        if (a.equals(x)) {
            a = "ROLE_MODPERF";
            b = "MODIFICAR PERFIL";
            insert_sql(a, b);
        }
        a = "CONSULTAR PERFIL";
        if (a.equals(x)) {
            a = "ROLE_CONPERF";
            b = "CONSULTAR PERFIL";
            insert_sql(a, b);
        }
        a = "CREAR USUARIO";
        if (a.equals(x)) {
            a = "ROLE_CREUSUA";
            b = "CREAR USUARIO";
            insert_sql(a, b);
        }
        a = "MODIFICAR DATOS PERSONALES";
        if (a.equals(x)) {
            a = "ROLE_MODUSUA";
            b = "MODIFICAR DATOS PERSONALES";
            insert_sql(a, b);
        }
         a = "MODIFICAR PERFIL USUARIO";
        if (a.equals(x)) {
            a = "ROLE_MODPERUSU";
            b = "MODIFICAR PERFIL USUARIO";
            insert_sql(a, b);
        }
        a = "ESTADO DE USUARIO";
        if (a.equals(x)) {
            a = "ROLE_MODESTAUSU";
            b = "ESTADO DE USUARIO";
            insert_sql(a, b);
        }
        a = "CONSULTAR USUARIO";
        if (a.equals(x)) {
            a = "ROLE_CONSUSUA";
            b = "CONSULTAR USUARIO";
            insert_sql(a, b);
        }
        a = "CREAR MATRIZ DE RECORRIDOS";
        if (a.equals(x)) {
            a = "ROLE_CREARMATRIZ";
            b = "CREAR MATRIZ DE RECORRIDOS";
            insert_sql(a, b);
        }
        a = "CONSULTAR MATRIZ DE RECORRIDOS";
        if (a.equals(x)) {
            a = "ROLE_CONSMATRIZ";
            b = "CONSULTAR MATRIZ DE RECORRIDOS";
            insert_sql(a, b);
        }
        a = "CREAR REPORTE";
        if (a.equals(x)) {
            a = "ROLE_CREAREPO";
            b = "CREAR REPORTE";
            insert_sql(a, b);
        }
        a = "MODIFICAR REPORTE";
        if (a.equals(x)) {
            a = "ROLE_MODREPO";
            b = "MODIFICAR REPORTE";
            insert_sql(a, b);
        }
        a = "ASIGNAR ESTADO EXPEDIENTE";
        if (a.equals(x)) {
            a = "ROLE_ESTADOEXPE";
            b = "ASIGNAR ESTADO EXPEDIENTE";
            insert_sql(a, b);
        }
        a = "MODIFICAR MANTENIMIENTO";
        if (a.equals(x)) {
            a = "ROLE_MANTO";
            b = "MODIFICAR MANTENIMIENTO";
            insert_sql(a, b);
        }
        a = "CONSULTAR AUDITORIA";
        if (a.equals(x)) {
            a = "ROLE_AUDIO";
            b = "CONSULTAR AUDITORIA";
            insert_sql(a, b);
        }
        a = "ASIGNAR VISTO BUENO";
        if (a.equals(x)) {
            a = "ROLE_VISTOB";
            b = "ASIGNAR VISTO BUENO";
            insert_sql(a, b);
        }

    }




public void borrar(){
    String sql="";
    sql="DELETE FROM PERFIL_AUTORIDAD WHERE idperfil = "+this.perfilSeleccionado+" ";
    jdbcTemplate.execute(sql);
}

public void insert_sql(String s, String z) {
    String sql="";
    //System.out.println(this.perfilSeleccionado);
    sql="INSERT INTO PERFIL_AUTORIDAD VALUES (nextval('perfil_autoridadseq'),"+this.perfilSeleccionado+",'"+s+"','"+z+"')";
    jdbcTemplate.execute(sql);
    }

public void entrar_sql(String s) {
    String sql="";
    //System.out.println(this.perfilSeleccionado);
    sql="INSERT INTO PERFIL_AUTORIDAD VALUES (nextval('perfil_autoridadseq'),"+this.perfilSeleccionado+",'"+s+"')";
    jdbcTemplate.execute(sql);
    }

public String cargarlista(){
    String destino="irmodificarperfil";
    return destino;
}


 public Result getListaTemporal() throws Exception  {
      
        ConexionServidor miConexion = new ConexionServidor();
        Connection conn = miConexion.getConnection();
        PreparedStatement ps;
        try {
            String sql = "select nombreautoridad from perfil_autoridad where idperfil = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,this.perfilSeleccionado);
             ResultSet rs = ps.executeQuery();
             
         return ResultSupport.toResult(rs);
        } finally {
            conn.close();
        }
 
 }


 String nombreperfil;

    public String getNombreperfil() {
        return nombreperfil;
    }

    public void setNombreperfil(String nombreperfil) {
        this.nombreperfil = nombreperfil;
    }


 public  void obtenerNombrePerfil(){
      ConexionServidor myConexion = new ConexionServidor();
         try {
            Connection con = myConexion.getConnection();
            PreparedStatement ps = con.prepareStatement("select nombreperfil from perfil where idperfil = ?");
            ps.setInt(1,this.perfilSeleccionado);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                nombreperfil = rs.getString("nombreperfil");
                System.out.println(nombreperfil);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }





}
